﻿using Liang.DataBase.DAL;
using Liang.DataBase.Model;
using Liang.DataBase.Model.Entity;
using Liang.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Liang.DataBase.IDAL
{
    /// <summary>
    /// 数据访问层基类接口
    /// </summary>
    /// <typeparam name="T">实体对象类型</typeparam>
    public interface IBaseDAL<T> where T : class
    {
        /// <summary>
        /// 插入指定对象到数据库中
        /// </summary>
        /// <param name="t">指定的对象</param>
        /// <returns>执行成功返回<c>true</c>，否则为<c>false</c></returns>
        bool Insert(T t);

        // <summary>
        /// 根据指定对象的ID,从数据库中删除指定对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>执行成功返回<c>true</c>，否则为<c>false</c>。</returns>
        bool Delete(object id);

        /// <summary>
        /// 从数据库中删除指定对象
        /// </summary>
        /// <param name="id">指定对象</param>
        /// <returns>执行成功返回<c>true</c>，否则为<c>false</c>。</returns>
        bool Delete(T t);

        /// <summary>
        /// 更新对象属性到数据库中
        /// </summary>
        /// <param name="t">指定的对象</param>
        /// <param name="key">主键的值</param>
        /// <returns>执行成功返回<c>true</c>，否则为<c>false</c></returns>
        bool Update(T t, object key);

        /// <summary>
        /// 查询数据库,返回指定ID的对象
        /// </summary>
        /// <param name="id">ID主键的值</param>
        /// <returns>存在则返回指定的对象,否则返回Null</returns>
        T FindByID(object id);

        /// <summary>
        /// 根据条件查询数据库,如果存在返回第一个对象
        /// </summary>
        /// <param name="match">条件表达式</param>
        /// <returns>存在则返回指定的第一个对象,否则返回默认值</returns>
        T FindSingle(Expression<Func<T, bool>> match);


        /// <summary>
        /// 返回可查询的记录
        /// </summary>
        /// <returns></returns>
        IList<T> FindAll();

        /// <summary>
        /// 根据条件查找返回记录
        /// </summary>
        /// <param name="match"></param>
        /// <returns></returns>
        IList<T> FindAll(Expression<Func<T, bool>> match);

        /// <summary>
        /// 根据条件表达式返回可查询的记录
        /// </summary>
        /// <param name="match">查询条件</param>
        /// <param name="orderByProperty">排序表达式</param>
        /// <param name="isDescending">如果为true则为降序，否则为升序</param>
        /// <returns></returns>
        IList<T> FindWithOrderby(Expression<Func<T, bool>> match, string propertyName, bool isDesc);


        /// <summary>
        /// 根据条件查询数据库，并按指定条件排序
        /// </summary>
        /// <param name="match">条件表达式树</param>
        /// <param name="info">分页信息</param>
        /// <param name="propertyName">排序列</param>
        /// <param name="isDesc">是否降序</param>
        /// <returns></returns>
        IList<T> FindWithPagerAndOrderby(Expression<Func<T, bool>> match, ref PageInfo info, string propertyName, bool isDesc);
    }
}
